class ItemDeleteController < ApplicationController
  #before_filter :login_required,:except=>[:index,:show,:dialog]
  
  def index
    @items = Item.all
  end

  def show
    @recommend_subjects ||= RecommendSubject.active
    @item = Item.find(params[:id])
    @tags = @item.item_tags
    @pictures = @item.pictures.limit(4)
    @short_reviews = @item.short_reviews.limit(5)
    @hot_reviews = @item.hot_reviews.limit(5)
    @latest_reviews = @item.latest_reviews.limit(5)
    @relate_user_list = Item.related_user(params[:id],4)      
      
  end

  #推荐
  def recommend
     render :text => "success"
  end
  #收藏
  def collect
     render :text => "success"
  end
  
  #底部评论
  def register_comment
    #ItemReview.transaction do
      #评论
      ItemReview.create do |re|
          re.user_id=0
          re.item_id=params[:id]
          re.content=params[:comment]
          re.up_total = 0
          re.down_total=0
          re.tags=params[:tags]
          re.impression=params[:impressions]
      end
      #标签
      ItemTag.crt_tags(params[:tags],params[:id])
      #评分
      if params[:interest]=='collect'
        params[:rate][:user_id]=0
        params[:rate][:item_id]=params[:id]
        ItemRating.create(params[:rate])
      end#评分
    
    #end#transaction
    redirect_to(item_url)
  end
  
  def reviews
    @reviews = Item.find(params[:id]).hot_reviews
  end
  
  #回复评论
  def replay
    ItemReviewReply.create do |rep|
      rep.user_id=0
      rep.item_review_id=params[:review_id]
      rep.content = params[:content]
    end
    
    redirect_to(item_url)
  end
  
  #想买/买了
  def interest
    if params[:interest].eql? 'wish'
      #想买
      if params[:foldcollect].eql? 'U'
        #展开
        #ItemReview.transaction do
          #评论
          ItemReview.create do |re|
              re.user_id=0
              re.item_id=params[:id]
              re.content=params[:comment]
              re.up_total = 0
              re.down_total=0
              re.tags=params[:tags]
              re.impression=params[:impressions]
              re.flag=1
          end if !params[:comment].blank?|| !params[:tags].blank?|| !params[:impressions].blank?
          #标签
          ItemTag.crt_tags(params[:tags],params[:id]) if !params[:tags].blank?

        #end#transaction
      else
        #收起
      end
    else
      #买过了
      if params[:foldcollect].eql? 'U' 
        #ItemReview.transaction do
          #评论
          ItemReview.create do |re|
              re.user_id=0
              re.item_id=params[:id]
              re.content=params[:comment]
              re.up_total = 0
              re.down_total=0
              re.tags=params[:tags]
              re.impression=params[:impressions]
              re.flag=1
          end if !params[:comment].blank?|| !params[:tags].blank?|| !params[:impressions].blank?
          #标签
          ItemTag.crt_tags(params[:tags],params[:id]) if !params[:tags].blank?
          #评分
          ItemRating.create do |rate|
            rate.user_id=0
            rate.item_id=params[:id]
            rate.global_rating=params[:n_rating]
            rate.flag=1
          end

        #end#transaction
      else
        
      end
    end
    render :text => "success"
  end
  
  #返回对话框
  def dialog
    #if !self.current_user
    #  render :text => "login_required"
    #else
      @item = Item.find(params[:id])
      @tags = @item.item_tags
      if params[:t]=='1'
          params[:form_type]='interest'
      elsif params[:t]=='2'
          params[:form_type]='recommend'
      elsif params[:t]=='3'
          params[:form_type]='collect'
          @my_collect = [UserFavoriteSubject.new do |us| us.id=1;us.title='衣柜' end]
      end

      render :layout => false    
    #end
  end
  
  #顶/踩
  def up_down
    is_rept = ItemReviewVote.find_by_item_review_id_and_user_id(params[:review_id],0)
    if is_rept
      render :text => "rept"
    else
      ItemReviewVote.create do |vote|
        vote.item_review_id = params[:review_id]
        vote.user_id=0
        vote.vote = params[:vote] 
      end
      if params[:vote].eql? 'up'
        ItemReview.update_counters(params[:review_id],:up_total=>1)
      else
        ItemReview.update_counters(params[:review_id],:down_total=>1)
      end
      render :text => "succ"
    end
  end
  

end
